package com.wwp.tbshop.view.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.wwp.tbshop.R;
import com.wwp.tbshop.activity.AddOrEditAddressActivity;
import com.wwp.tbshop.activity.AddressManageActivity;
import com.wwp.tbshop.model.bean.AddressList;
import com.wwp.tbshop.presenter.AddressChangePresenter;
import com.wwp.tbshop.util.Constant;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Xiaokun on 2017/6/15.
 */

public class AddressRVAdapter extends RecyclerView.Adapter {

    private int userId;
    private Context mContext;
    private List<AddressList.AddressListBean> mAddressListBean;
    private AddressChangePresenter mPresenter;

    public AddressRVAdapter(Context context) {
        this.mContext = context;
    }

    private AddressList.AddressListBean currentSet;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_address, parent, false);
        AddressListHolder listHolder = new AddressListHolder(view);
        return listHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        AddressListHolder listHolder = (AddressListHolder) holder;
        listHolder.setData(mAddressListBean.get(position));
    }

    @Override
    public int getItemCount() {
        if (mAddressListBean != null && mAddressListBean.size() > 0) {
            return mAddressListBean.size();
        }
        return 0;
    }

    public void setDatas(AddressList addressList) {
        mAddressListBean = addressList.getAddressList();
    }

    public void onSetDefaultResult() {
        if (currentSet!=null){
            currentSet.setIsDefault(Constant.IS_DEFAULT_ADDRESS);
            for (AddressList.AddressListBean listBean : mAddressListBean) {
                if (listBean.getId() != currentSet.getId()) {
                    Log.e("eeeeeeeeeeee", "onCheckedChanged: " + listBean.getId() + "#" + listBean.getIsDefault());
                    listBean.setIsDefault(Constant.NO_DEFAULT_ADDRESS);
                    Log.e("xxxxxxxxxxxx", "onCheckedChanged: " + listBean.getId() + "#" + listBean.getIsDefault());
                }
            }
        }
        notifyDataSetChanged();
    }

    public void setUserid(int userId) {
        this.userId=userId;
    }


    public class AddressListHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_address_user)
        TextView tvAddressUser;
        @Bind(R.id.tv_address_phone)
        TextView tvAddressPhone;
        @Bind(R.id.tv_address_detail)
        TextView tvAddressDetail;
        @Bind(R.id.cb_default)
        CheckBox cbDefault;
        @Bind(R.id.tv_address_edit)
        TextView tvAddressEdit;
        @Bind(R.id.tv_address_delete)
        TextView tvAddressDelete;

        private AddressList.AddressListBean mAddressbean;

        @OnClick({R.id.tv_address_edit, R.id.tv_address_delete})
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tv_address_edit:
                    Intent intent = new Intent(mContext, AddOrEditAddressActivity.class);
                    intent.putExtra("mAddressBean", mAddressbean);
                    ((AddressManageActivity) mContext).startActivityForResult(intent,AddressManageActivity.REQUEST_CODE);
                    break;
                case R.id.tv_address_delete:
                    deleteAddress();
                    break;
            }
        }

        private void deleteAddress() {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setMessage("确定删除地址?");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    new AddressChangePresenter(((AddressManageActivity) mContext)).deleteAddress(mAddressbean.getId(), userId);
                    mAddressListBean.remove(mAddressbean);
                    notifyDataSetChanged();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            builder.show();
        }

        AddressListHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(AddressList.AddressListBean addressListBean) {
            this.mAddressbean = addressListBean;
            tvAddressUser.setText(addressListBean.getName());
            tvAddressPhone.setText(addressListBean.getPhoneNumber());
            tvAddressDetail.setText(addressListBean.getProvince() + addressListBean.getCity() + addressListBean.getAddressArea() + addressListBean.getAddressDetail());
            cbDefault.setOnCheckedChangeListener(null);

            if (addressListBean.getIsDefault() == Constant.IS_DEFAULT_ADDRESS) {
                cbDefault.setChecked(true);
                cbDefault.setEnabled(false);
            } else {
                cbDefault.setChecked(false);
                cbDefault.setEnabled(true);
            }

            cbDefault.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if (isChecked = true) {
                        mPresenter = new AddressChangePresenter(((AddressManageActivity) mContext));
                        mPresenter.setAddressDefault(mAddressbean.getId(), userId);
                        currentSet = mAddressbean;
//                        mAddressbean.setIsDefault(Constant.IS_DEFAULT_ADDRESS);
                    }
                }
            });
        }
    }

}
